﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Pyramid_Game
{
    public class QuestionBat : GameAnimation
    {
        Texture2D texture;
        Random rand = new Random(Int32.Parse(DateTime.Now.ToString("HHmmss")));

        int type;
        int X, Y;
        int width, height;
        double scale = 1;

        bool size = false;
        bool location = false;

        public QuestionBat(int type, int width, int height)
        {
            this.type = type;
            this.width = width;
            this.height = height;
            this.X = 0;
            this.Y = 0;
        }

        public override void LoadContent(ContentManager content)
        {
            texture = content.Load<Texture2D>("Texture/Bats/batAnimation");
            //texture = content.Load<Texture2D>("Texture/Bats/batAnimation" + type);

            animationCollection.Add("Fly", new Animation(texture, 150, "Fly", true, 0.05f));
            currentAnimation = "Fly";
            PlayAnimation("Fly");
        }

        public override void Update(Touch touch, GameTime gameTime)
        {
            if (GameConstants.stage == 1)
            {
                Stage01(gameTime);
            }
            else if (GameConstants.stage == 2)
            {
                Stage02(gameTime);
            }
            else if (GameConstants.stage == 3)
            {
                Stage03(gameTime);
            }

            Initialize(new Rectangle(X, Y, (int)(width * scale), (int)(height * scale)));

            base.Update(touch, gameTime);
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
        }

        public void Stage01(GameTime gameTime)
        {
            SizeControl();

            X = (int)(Game1.viewport.Width / 2 - width * scale / 2);
            Y = (int)(height / 2 - height * scale / 2);
        }
        public void Stage02(GameTime gameTime)
        {
            int move = 5;

            SizeControl();

            if (location)
            {
                X += move;
                if (X >= Game1.viewport.Width - width)
                    location = false;
            }
            else
            {
                X -= move;
                if (X <= 0)
                    location = true;
            }
             
            Y = (int)(height / 2 - height * scale / 2);
        }
        public void Stage03(GameTime gameTime)
        {
            int move = 20;

            SizeControl();

            int num = rand.Next(0, 3);
            if (num == 0)
            {
                X += move;
            }
            else if(num == 1)
            {
                X -= move;
            }
            num = rand.Next(0, 3);
            if (num == 0)
            {
                Y += move;
            }
            else if (num == 1)
            {
                Y -= move;
            }
            num = rand.Next(0, 31);
            if (num == 0)
            {
                X = rand.Next(0, Game1.viewport.Width - width);
                Y = rand.Next(0, Game1.viewport.Height - height);
            }

            X = (int)MathHelper.Clamp(X, 0, Game1.viewport.Width - width);
            Y = (int)MathHelper.Clamp(Y, 0, Game1.viewport.Height - height);
        }

        public void SizeControl()
        {
            if (size)
            {
                scale += 0.01;
                if (scale >= 1)
                   size = false;
            }
            else
            {
                scale -= 0.01;
                if (scale <= 0.1)
                    size = true;
            }
        }
    }
}
